package com.jysz.certificate.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.system.FinancialDao;
import com.jysz.certificate.persistence.dao.system.TreatyDao;
import com.jysz.certificate.persistence.model.dto.FinancialDTO;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.persistence.model.system.Treaty;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class FinancialService extends EntityService<Financial, Long, FinancialDao> {
	
	@Resource
	private FinancialDao financialDao;
	@Resource
	private TreatyDao treatyDao;
	
	
	public Financial getFinancial(Long id){
		return financialDao.find(id);
	}
	
	public void delete(Long id){
		Financial f = this.find(id);
		f.setIsDelete("1");
		financialDao.save(f);
	}
	public boolean getPass(String name,Long id){
		Criteria criteria = financialDao.createCriteria();
		criteria.add(Restrictions.eq("name", name));
		if(id!=null){
			criteria.add(Restrictions.ne("id", id));
		}
		Financial f = (Financial) criteria.uniqueResult();
		return f==null?true:false;
	}
	public Page<Financial> selectFinancial(FinancialDTO search,int currentPage,int pageSize){
		Criteria criteria = financialDao.createCriteria();
		criteria.add(Restrictions.eq("isDelete", "0"));
		if(search!=null){
			if(null!=search.getName()&&!"".equals(search.getName())){
				criteria.add(Restrictions.like("name", search.getName(), MatchMode.ANYWHERE));
			}
			if(null!=search.getManager()&&!"".equals(search.getManager())){
				criteria.add(Restrictions.like("manager", search.getManager(), MatchMode.ANYWHERE));
			}
			if(!"-1".equals(search.getBilling())){
				criteria.add(Restrictions.eq("billing", search.getBilling()));
			}
		}
		return this.findPage(criteria, currentPage, pageSize);
	}
	
	public List<Financial> listFinancial(){
		return this.findAll();
	}
	
	@Override
	public FinancialDao getEntityDao() {
		return financialDao;
	}

	public void update(Financial f) {
		financialDao.save(f);
	}
	
	public List<Financial> finList(){
		Criteria criteria=financialDao.createCriteria();
		criteria.add(Restrictions.eq("isDelete", "0"));//删除标识
		return  criteria.list();
	}

	/**
	 * 获得指定店铺签订的协议中允许预收的金融机构
	 * @author xuxiaolong
	 * @param shopId
	 * @return
	 */
	public List<Financial> findAdvanceByShop(Long shopId,String collectionWay){
		Criteria criteria=treatyDao.createCriteria();
		criteria.createCriteria("shop").add(Restrictions.eq("id", shopId));
		criteria.add(Restrictions.eq("unitPrice", 0D));//单证释放价=0
		criteria.add(Restrictions.eq("collectionWay", collectionWay));//收取方式
		criteria.add(Restrictions.eq("state", true));//激活的
		criteria.createCriteria("financial").add(Restrictions.eq("billing", "1"));//非单独开票的
		List<Treaty> treatyList=criteria.list();
		List<Financial> rusList=new ArrayList<Financial>();
		for(Treaty treaty:treatyList){
			rusList.add(treaty.getFinancial());
		}
		return rusList;
	}
	
	/**
	 * 获得指定店铺签订的协议的金融机构
	 * @author xuxiaolong
	 * @param shopId
	 * @return
	 */
	public List<Financial> findByShop(Long shopId){
		Criteria criteria=treatyDao.createCriteria();
		criteria.createCriteria("shop").add(Restrictions.eq("id", shopId));
		criteria.add(Restrictions.eq("state", true));//激活的
		List<Treaty> treatyList=criteria.list();
		List<Financial> rusList=new ArrayList<Financial>();
		for(Treaty treaty:treatyList){
			rusList.add(treaty.getFinancial());
		}
		return rusList;
	}
	/**
	 * 独立开票金融机构
	 * @return
	 */
	public List<Financial> independenceopenBill(){
		return financialDao.financialBilling();
	}
	/**
	 * 获得所有的金融机构
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String , Financial> financialAllMap(){
		Map<String , Financial> map = new HashMap<String, Financial>();
		Criteria criteria = financialDao.createCriteria();
		criteria.add(Restrictions.eq("isDelete", "0"));
		List<Financial> financials = criteria.list();
		if(financials.size() >0){
			for(Financial financial : financials){
//				map.put(financial.getName(), financial);
				map.put(financial.getBrief(), financial);
			}
		}
		return map;
	}
	
	public String fianName(Long id){
		 Financial financial = financialDao.get(id);
		 if(null != financial){
			 return financial.getName();
		 }
		return null;
	}

	public List<Financial> findAvailableAll() {
		return this.finList();
	}	
}
